package com.okq.action;

import com.okq.action.base.BaseActionSupport;
import com.okq.entity.Menu;
import com.okq.entity.User;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述 : 2017/3/15 11:55
 * 作者 : zdl
 * 日期 : 2017/6/28 11:00
 */
public class LoginAction extends BaseActionSupport {

    private static final long serialVersionUID = 1662587420591383672L;

    private StringBuffer showUrl = new StringBuffer();

    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(LoginAction.class);

    /**
     * 登陆后跳转
     *
     * @return SUCCESS
     */
    @SuppressWarnings("unchecked")
    public String loginForward() {
        try {
            User loginUser = (User) getSession().getAttribute("loginUser");
            if (loginUser != null) {
                // 获取保存的菜单信息
                List<Menu> menuList = new ArrayList<>();
                if (this.getSession().getAttribute("menuList") != null) {
                    menuList = (List<Menu>) this.getSession().getAttribute("menuList");
                }
                String menu = getMenuUrl(menuList);
                this.getSession().setAttribute("menu", menu);// 把菜单存入session
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return SUCCESS;
    }

    /**
     * 根据 list中的菜单转换成字符串
     *
     * @param menuList 菜单集合
     * @return 拼装后的菜单
     */
    private String getMenuUrl(List<Menu> menuList) {
        try {
            HttpServletRequest request = this.getRequest();
            String path = request.getContextPath();
            String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
            showUrl.append("<ul class=\"menu\">");
            List<Menu> listFirst = getMenuListByPId(menuList, "0");
            if (listFirst != null && !listFirst.isEmpty() && listFirst.size() > 0) {
                // 遍历第一层
                for (Menu m : listFirst) {
                    showUrl.append("<li class=\"level1\">");
                    List<Menu> listSecond = getMenuListByPId(menuList, m.getId());
                    String firstMenuCode = m.getMenuCode();
                    if ("1".equals(firstMenuCode)) {
                        showUrl.append("<a href=\"#\" class=\"click_to_action mr ").append(m.getMenuAttr()).append(" \" onclick=\"add($(this).html(),$(this),'")
                                .append(basePath).append(m.getUrl()).append("')\"><em class=\"iconfont\">").append(m.getIcon()).append("</em><cite>")
                                .append(m.getMenuName()).append("</cite></a>");
                    } else {
                        if (listSecond != null && !listSecond.isEmpty() && listSecond.size() > 0) {
                            showUrl.append("<a href=\"#\" class=\" ").append(m.getMenuAttr()).append(" \"><em class=\"iconfont\">").append(m.getIcon()).append("</em>")
                                    .append(m.getMenuName()).append("<i class=\"\"></i></a>");
                        } else {
                            showUrl.append("<a href=\"#\" class=\"click_to_action ").append(m.getMenuAttr()).append(" \" onclick=\"add($(this).html(),$(this),'")
                                    .append(basePath).append(m.getUrl()).append("')\"><em class=\"iconfont\">").append(m.getIcon()).append("</em><cite>")
                                    .append(m.getMenuName()).append("</cite></a>");
                        }
                    }
                    if (listSecond != null && !listSecond.isEmpty() && listSecond.size() > 0) {
                        showUrl.append("<ul class=\"level2\" style=\"display:none;\">");
                        // 遍历第二层
                        for (Menu m2 : listSecond) {
                            showUrl.append("<li>");
                            showUrl.append("<a href=\"#\" class=\"click_to_action ").append(m2.getMenuAttr()).append(" \" onclick=\"add($(this).html(),$(this),'")
                                    .append(basePath).append(m2.getUrl()).append("')\"><em class=\"iconfont\">").append(m2.getIcon()).append("</em><cite>")
                                    .append(m2.getMenuName()).append("</cite></a>");
                            showUrl.append("</li>");
                        }
                        showUrl.append("</ul>");
                    }
                    showUrl.append("</li>");
                }
            }
            showUrl.append("</ul>");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return showUrl.toString();
    }

    /**
     * 查询ParentId = pId 的所有菜单
     *
     * @param menuList 菜单集合
     * @param pId      父ID
     * @return 子菜单list
     */
    private List<Menu> getMenuListByPId(final List<Menu> menuList, String pId) {
        List<Menu> list = new ArrayList<>();
        try {
            if (menuList != null && !menuList.isEmpty() && menuList.size() > 0) {
                for (Menu menu : menuList) {
                    String parentId = menu.getParentId();
                    if (StringUtils.isNotBlank(parentId) && parentId.equals(pId)) {
                        list.add(menu);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

}
